package com.example.teamwork.Controller;

import com.example.teamwork.Shrio.Bean.ResponseBean;
import com.example.teamwork.informationGet.InterVo;
import com.example.teamwork.informationGet.PageVO;
import com.example.teamwork.pojo.Inter;
import com.example.teamwork.service.InterService;
import com.wuwenze.poi.ExcelKit;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.slf4j.MDC;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * @author  SongPeng
 * @date  2021/11/10 14:19
 * @version 1.0
 *
 */

@RestController
@RequestMapping("/Inter")
public class InterController {
    @ModelAttribute
    public void doString(){
        MDC.put("operateModuleName","内部人员管理模块");
    }
    @Autowired
    private InterService interService;

    private static final Logger logger = LogManager.getLogger(InterController.class);
    //查询内部人员信息列表
    @GetMapping("/listData")
    @RequiresPermissions("inter:select")
    @ResponseBody
    public ResponseBean listData(@RequestParam("pageNum") Integer pageNum, @RequestParam("pageSize") Integer pageSize,InterVo interVo){
        PageVO<InterVo> interList=interService.findInterList(pageNum,pageSize,interVo);
        return ResponseBean.success(interList);
    }
    /**
     * 删除内部人员
     *
     * */
    @DeleteMapping("/delete/{interId}")
    @RequiresPermissions("inter:delete")
    public ResponseBean deleteinter(@PathVariable("interId") Long interId){
        logger.info("删除内部人员");
        interService.deleteById(interId);
        return ResponseBean.success();


    }
    /**
     * 增加内部人员
     *
     * */
    @RequiresPermissions({"inter:add"})
    @PostMapping("/add")
    public ResponseBean add(@RequestBody @Validated InterVo interVo) {
        logger.info("增加内部人员");
        interService.add(interVo);
        return ResponseBean.success();
    }
    /**
     * 更新内部人员信息
     *
     * @param interId
     * @param interVo
     * @return
     */
    @RequiresPermissions({"inter:update"})
    @PutMapping("/update/{interId}")
    public ResponseBean update(@PathVariable Long interId, @RequestBody @Validated InterVo interVo) {
        logger.info("更新内部人员信息");
        interService.update(interId, interVo);
        return ResponseBean.success();
    }

    /**
     * 编辑内部人员
     * @param interId
     * @return
     */
    @RequiresPermissions({"inter:update"})
    @GetMapping("/edit/{interId}")
    public ResponseBean edit(@PathVariable Long interId) {
        logger.info("编辑内部人员");
        InterVo interVo = interService.edit(interId);
        return ResponseBean.success(interVo);
    }
    /**
     * 导出内部人员excel
     * @param response
     */
    @PostMapping("/excel")
    @RequiresPermissions("inter:update")
    public void export(HttpServletResponse response) {
        logger.info("导出内部人员表格");
        List<Inter> inters = this.interService.findAll();
        ExcelKit.$Export(Inter.class, response).downXlsx(inters, false);
    }

}
